package com.tengju.data.infrastructure.repository;

import com.tengju.data.domain.model.report.AchievementBonusCalMidInfo;
import com.tengju.data.domain.model.report.AchievementBonusCalMidInfoRepository;
import com.tengju.data.domain.model.report.AchievementBonusCalTypeEnum;
import com.tengju.data.infrastructure.persistence.mybatis.dataobject.AchievementBonusCalMidInfoDO;
import com.tengju.data.infrastructure.persistence.mybatis.mapper.data.custom.AchievementBonusCalMidInfoDOCustomMapper;
import com.tengju.data.infrastructure.repository.convert.AchievementBonusConvert;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;
import org.springframework.util.CollectionUtils;

import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @author chenhang
 * @Date 2020/12/28
 */
@Repository
@RequiredArgsConstructor(onConstructor_ = {@Autowired})
public class AchievementBonusCalMidInfoRepositoryImpl implements AchievementBonusCalMidInfoRepository {

    private final AchievementBonusCalMidInfoDOCustomMapper achievementBonusCalMidInfoDOCustomMapper;

    private static final Integer BATCH_QUERY_LIMIT = 1000;

    @Override
    public List<AchievementBonusCalMidInfo> listMidInfo(LocalDateTime logTime, Integer month, AchievementBonusCalTypeEnum calType) {
        int count = achievementBonusCalMidInfoDOCustomMapper.countByLogTimeAndType(logTime,calType.getType());
        if (count <= 0) {
            return Collections.emptyList();
        }
        List<AchievementBonusCalMidInfoDO> allInfos = new ArrayList<>();
        int offset = 0;
        while (offset <= count) {
            List<AchievementBonusCalMidInfoDO> midInfoVOList = achievementBonusCalMidInfoDOCustomMapper.listMidInfoByLogTimeAndType(logTime, calType.getType(), offset, BATCH_QUERY_LIMIT);
            offset += BATCH_QUERY_LIMIT;
            if (CollectionUtils.isEmpty(midInfoVOList)) {
                break;
            }
            allInfos.addAll(midInfoVOList);
        }
        return allInfos.stream()
                .map(AchievementBonusConvert::convertMidInfoVOToEntity)
                .collect(Collectors.toList());
    }

    @Override
    public void saveMarketProfitMidInfoRealtime(Integer month, LocalDateTime logTime) {
        achievementBonusCalMidInfoDOCustomMapper.saveMidInfoRealtime(month,logTime,AchievementBonusCalTypeEnum.REALTIME.getType());
    }

    @Override
    public void saveMarketProfitMidInfoMonthSnapshot(LocalDateTime logTime, Integer month) {
        achievementBonusCalMidInfoDOCustomMapper.saveMidInfoMonth(month,logTime,AchievementBonusCalTypeEnum.MONTH.getType());
    }

    @Override
    public void saveMarketProfitMidInfoMonthRisk(LocalDateTime logTime, Integer month) {
        achievementBonusCalMidInfoDOCustomMapper.saveMidInfoRisk(month,logTime,AchievementBonusCalTypeEnum.MONTH_RISK.getType());
    }

    @Override
    public List<LocalDateTime> listLogtimeBeforeClearDate(LocalDateTime clearDate) {
        return achievementBonusCalMidInfoDOCustomMapper.listLogtimeBeforeClearDate(clearDate);
    }

    @Override
    public void clearHistoryRecord(LocalDateTime logTime) {
        achievementBonusCalMidInfoDOCustomMapper.clearHistoryRecord(logTime);
    }
}
